package com.perfma.xtest.request;

import java.io.*;

public class FileTraversal {
    public static void main(String[] args) {
        String folderPath = "/Users/sijunhui/Downloads/log/pid文件/29.16.16.132/resources";  // 替换为实际的文件夹路径
        traverseFolder(new File(folderPath));
    }

    public static void traverseFolder(File folder) {
        if (folder == null || !folder.exists()) {
            System.out.println("文件夹不存在！");
            return;
        }

        if (folder.isFile()) {
            processFile(folder);
        } else if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    traverseFolder(file);
                }
            }
        }
    }

    public static void processFile(File file) {
        System.out.println("进行中: " + file.getName());
        if (file.isFile()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                String line;
                int lineNumber = 1;
                while ((line = reader.readLine()) != null) {
                    if (lineNumber < getTotalLines(file) && line.trim().isEmpty()) {
                        System.out.println("文件名: " + file.getName());
                        break;
                    }
                    lineNumber++;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static int getTotalLines(File file) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            int lines = 0;
            while (reader.readLine() != null) {
                lines++;
            }
            return lines;
        }
    }
}